package com.dure.hotmemory.controllers;

import android.text.TextUtils;


import com.dure.commonlib.util.PreferencesUtils;
import com.dure.commonlib.util.ToastUtils;
import com.dure.hotmemory.bean.Bean;
import com.dure.hotmemory.bean.FindPwd;
import com.dure.hotmemory.bean.UserInfo;
import com.dure.hotmemory.config.Config;
import com.dure.hotmemory.scenes.HomeScene;
import com.dure.hotmemory.scenes.PayScene;

import java.util.HashMap;

/**
 * Created by dure on 2017/4/17.
 */

public class HomeController extends Controller {


    HomeScene homeScene;
    public HomeController(HomeScene homeScene) {
        super(homeScene);
        this.homeScene = homeScene;
    }

    public void changeToPay(String amountStr, String pay_type) {
        double amount = 0;
        if (!TextUtils.isEmpty(amountStr)) {
            amount = Double.valueOf(amountStr);
        }

        PayScene payScene = new PayScene(baseSurfaceView);
        payScene.setPayType(pay_type);
        payScene.setAmount(amount);
        activity.moveToNewScene(payScene);
    }

    @Override
    public void handleRequestSuccess(Bean bean) {
        if (bean instanceof UserInfo) {
            UserInfo userInfo = (UserInfo) bean;
            if (userInfo.status == 1) {

                handleLoginSuccess(userInfo);

                homeScene.changeLoginState();

            } else {
                PreferencesUtils.putBoolean(activity, "login_success", false);
                ToastUtils.show(activity, userInfo.msg);
            }
        } else if (bean instanceof FindPwd) {
            FindPwd findPwd = (FindPwd) bean;
            ToastUtils.show(activity, findPwd.msg);
        }
    }

    private void handleLoginSuccess(UserInfo userInfo){

        if (userInfo.data != null && userInfo.data.size() > 0) {
            PreferencesUtils.putBoolean(activity, "login_success", true);
            ToastUtils.show(activity, "登录成功");

            PreferencesUtils.putString(activity, "name", userInfo.data.get(0).name);
            if (!TextUtils.isEmpty(userInfo.data.get(0).phone))
                PreferencesUtils.putString(activity, "phone", userInfo.data.get(0).phone);

            if (!TextUtils.isEmpty(userInfo.data.get(0).uid)) {
                PreferencesUtils.putString(activity, "uid", userInfo.data.get(0).uid);
            }
            PreferencesUtils.putString(activity, "head_img", userInfo.data.get(0).image);
            int pay_state = 0;
            if (userInfo.pay != null && userInfo.pay.size() > 0) {
                pay_state = userInfo.pay.get(0).pay_state;
            }
            if (pay_state == 1) {// 用户己支付
                PreferencesUtils.putBoolean(activity, "paied", true);

                if (userInfo.pay.get(0).ad_state == 1) {
                    PreferencesUtils.putBoolean(activity, "move_ad", true);
                } else {// 用户支付了，但是没有选择去广告
                    PreferencesUtils.putBoolean(activity, "move_ad", false);
                }

            } else {
                PreferencesUtils.putBoolean(activity, "paied", false);
            }

//            if (TextUtils.isEmpty(userInfo.data.get(0).is_spread))
//                userInfo.data.get(0).is_spread = "0";

            PreferencesUtils.putInt(activity, "is_spread", userInfo.data.get(0).is_spread);
        } else {// 三方第一次登录获取信息返回
            // 没有获取到数据， 将数据上传服务器
            String uid = PreferencesUtils.getString(activity, "uid");
            String name = PreferencesUtils.getString(activity, "name");
            String sex = PreferencesUtils.getString(activity, "sex");
            String head_img = PreferencesUtils.getString(activity, "head_img");
            insertThirdLoginUserInfo(uid, name, sex, head_img);
            ToastUtils.show(activity, "登录成功");
        }
    }


    // 将用户数据重置为初始状态
    // 等以后用户的数据上传后， 将所有跟用户有关的个人数据的key加上用户的手机phone + key的形式， 避免切换用户数据用的还是上一个用户的情况
    public void resetUserData() {
        PreferencesUtils.putBoolean(activity, "login_success", false);
        PreferencesUtils.putBoolean(activity, "paied", false);
        PreferencesUtils.putBoolean(activity, "move_ad", false);
        PreferencesUtils.putString(activity, "phone", "");
        PreferencesUtils.putString(activity, "uid", "");
        PreferencesUtils.putString(activity, "name", "");
        PreferencesUtils.putString(activity, "head_img", "");
    }

}
